﻿using System;
using System.Globalization;
using System.IO;
using System.Runtime.Remoting;
using GurLoader.View;
using NUnit.Framework;

namespace GurLoaderWPF.Test.View
{
    [TestFixture]
    public class AutoElipseTextConverterTests
    {
        [Test]
        public void CreateInstanceTest()
        {
            var conv = new AutoElipseTextConverter();
            Assert.IsNotNull(conv);
        }

        [Test]
        public void NullValueTest()
        {
            var conv = new AutoElipseTextConverter();
            Assert.IsNotNull(conv);

            string filePath = null;

            string expected = string.Empty;

            string result = conv.Convert(filePath, typeof(String), null, CultureInfo.InvariantCulture) as string;

            if (result == null)
                Assert.Fail("Result was null.");

            Assert.AreEqual(expected, result);
        }

        [Test]
        public void EmptyValueTest()
        {
            var conv = new AutoElipseTextConverter();
            Assert.IsNotNull(conv);

            string filePath = string.Empty;

            string expected = string.Empty;

            string result = conv.Convert(filePath, typeof(String), null, CultureInfo.InvariantCulture) as string;

            if (result == null)
                Assert.Fail("Result was null.");

            Assert.AreEqual(expected, result);
        }

        [Test]
        public void ShortTextTest()
        {
            var conv = new AutoElipseTextConverter();
            Assert.IsNotNull(conv);

            string filePath = "C:\\User\\s.txt";

            string expected = filePath;

            string result = conv.Convert(filePath, typeof(String), null, CultureInfo.InvariantCulture) as string;

            if (result == null)
                Assert.Fail("Result was null.");

            Assert.AreEqual(expected, result);
        }

        [Test]
        public void StandardElipseTest()
        {
            var conv = new AutoElipseTextConverter();
            Assert.IsNotNull(conv);

            string filePath =
                "C:\\User\\MyUserName\\Documents\\TextFiles\\file.txt";

            string expected = "C:\\User\\MyUserName\\Documents" + "..." + Path.DirectorySeparatorChar + "file.txt";

            string result = conv.Convert(filePath, typeof(String), null, CultureInfo.InvariantCulture) as string;

            if (result == null)
                Assert.Fail("Result was null.");

            Assert.AreEqual(expected, result);
        }

        [Test]
        public void ElipseWithPathSeparatorOnBoundaryTest()
        {
            var conv = new AutoElipseTextConverter();
            Assert.IsNotNull(conv);

            string filePath =
                "C:\\User\\MyAUserName\\Documents\\aaaabbbbcccc.txt";

            string expected = "C:\\User\\MyAUserName" + "..." + Path.DirectorySeparatorChar + "aaaabbbbcccc.txt";

            string result = conv.Convert(filePath, typeof(String), null, CultureInfo.InvariantCulture) as string;

            if (result == null)
                Assert.Fail("Result was null.");

            Assert.AreEqual(expected, result);
        }

        [Test]
        public void VeryLongFilenameTest()
        {
            // should guarantee that at least the first 5 chars
            // of the path are available + the elipse portion +
            // whatever is left of the filename. This will mean
            // the last 31 chars of the filename if its excessively
            // long ( 5 start chars + 4 elipse chars )
            var conv = new AutoElipseTextConverter();
            Assert.IsNotNull(conv);

            string filePath =
                "C:\\User\\MyUserName\\Documents\\Other\\Directory\\some-file-that-has-an-excessively-long-filename-for-some-reason-that-i-dont-know.txt";

            string expected = "C:\\Us" + "..." + "ome-reason-that-i-dont-know.txt";

            string result = conv.Convert(filePath, typeof (String), null, CultureInfo.InvariantCulture) as string;

            if (result == null)
                Assert.Fail("Result was null.");

            Assert.AreEqual(expected, result);
        }

        [Test]
        [ExpectedException(typeof(NotImplementedException))]
        public void ConvertBackTest()
        {
            var conv = new AutoElipseTextConverter();
            Assert.IsNotNull(conv);

            conv.ConvertBack(new object(), typeof (String), null, CultureInfo.InvariantCulture);
        }
    }
}
